/*
 * @Date: 2023-01-22 02:17:37
 * @LastEditors: 恶名扬 2080156129@qq.com
 * @LastEditTime: 2023-01-24 02:27:15
 * @FilePath: \campus-repair-platform\server\service\common\announce.js
 */
/**
 * getAnnounceList    获取公告列表
 * queryAnnounceList  根据title模糊查询公告
 * getInfoAnnounce    获取公告信息
 */

const announceStatement = require("../../common/sql_sentences/announcement")

exports.getAnnounceList = async (req, res) => {
  try {
    const { scope, page, pagesize } = req.query
    const { type } = req.auth
    // 获取公告总数
    let total,
      sqlStr = announceStatement.selectCount({ scope }, type)
    let results = await RunSQL(sqlStr)
    total = results[0].total

    // 获取公告列表
    sqlStr = announceStatement.selectLimit({ scope }, page, pagesize, type)
    results = await RunSQL(sqlStr)
    return res.successSend('', 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.queryAnnounceList = async (req, res) => {
  try {
    const { title, page, pagesize } = req.query
    const { type } = req.auth
    // 查找公告
    let sqlStr = announceStatement.selectCountLike(title, type)
    console.log(sqlStr)
    let results = await RunSQL(sqlStr)
    if (!results.length) return res.successSend("没有找到公告！", 10005)
    total = results[0].total

    // 根据title模糊查询获取公告列表
    sqlStr = announceStatement.selectLikeLimit(title, page, pagesize, type)
    results = await RunSQL(sqlStr)
    return res.successSend("", 10000, { row: results, total })
  } catch (error) {
    res.errSend(error)
  }
}

exports.getInfoAnnounce = async (req, res) => {
  try {
    // 获取公告信息
    const sqlStr = announceStatement.idSelect(req.params.id)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1) return res.successSend("获取公告信息失败！", 10005)
    return res.successSend("", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}
